﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="PostureGestureController.cs" company="arvystate.net">
//   arvystate.net
// </copyright>
// <summary>
//   Gesture controller uses Kinect Toolkit library to recognize gestures
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace PresentationAid.Lib.GestureControllers
{
    using Kinect.Toolbox;

    using Microsoft.Kinect;

    using NLog;

    using PresentationAid.Lib.Enums;
    using PresentationAid.Lib.Gestures;

    /// <summary>
    /// Gesture controller uses Kinect Toolkit library to recognize gestures
    /// </summary>
    public sealed class PostureGestureController : DelayGestureController
    {
        #region Static Fields

        /// <summary>
        /// The logger.
        /// </summary>
        private static readonly Logger Logger = LogManager.GetCurrentClassLogger();

        #endregion

        #region Fields

        /// <summary>
        /// The _multiple posture detector.
        /// </summary>
        private readonly MultiplePostureDetector multiplePostureDetector;

        /// <summary>
        /// The _skeletons.
        /// </summary>
        private Skeleton[] skeletons;

        #endregion

        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="PostureGestureController"/> class.
        /// </summary>
        public PostureGestureController()
        {
            Logger.Trace("Init: PostureGestureController");

            this.multiplePostureDetector = new MultiplePostureDetector();
            this.multiplePostureDetector.PostureDetected += this.OnPostureDetected;
        }

        #endregion

        #region Public Methods and Operators

        /// <summary>
        /// Starts recognition engine on controller
        /// </summary>
        /// <param name="frame">
        /// SkeletonFrame with skeletons
        /// </param>
        public override void Recognize(SkeletonFrame frame)
        {
            frame.GetSkeletons(ref this.skeletons);

            foreach (Skeleton skeleton in this.skeletons)
            {
                if (skeleton.TrackingState != SkeletonTrackingState.Tracked)
                {
                    continue;
                }

                this.multiplePostureDetector.TrackPostures(skeleton);
            }
        }

        #endregion

        #region Methods

        /// <summary>
        /// The on posture detected.
        /// </summary>
        /// <param name="posture">
        /// The posture.
        /// </param>
        /// <param name="trackingId">
        /// The tracking id.
        /// </param>
        private void OnPostureDetected(string posture, int trackingId)
        {
            Logger.Debug("EventDetail: PostureDetected - Posture: {0}", posture);

            if ((posture == "LeftHello") || (posture == "RightHello") || (posture == "LeftHandOverHead")
                || (posture == "RightHandOverHead"))
            {
                this.ExecuteGestureRecognized(GestureCommand.Wave, trackingId);
            }
            else if (posture == "HandsJoined")
            {
                this.ExecuteGestureRecognized(GestureCommand.JoinedHands, trackingId);
            }
        }

        #endregion
    }
}